﻿using System;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace BaiTu.DAL.EF
{
    /// <summary>
    /// 默认约定 设置默认的字符串长度为50 默认的主键为ID
    /// </summary>
    public class DefaultTableConvention : Convention
    {
        public DefaultTableConvention()
        {
            this.Properties<string>().Configure(t => t.HasMaxLength(50));
            this.Properties<DateTime>().Configure(t => t.HasColumnType("datetime2"));
            this.Properties().Where(t => t.Name.Equals("id", StringComparison.CurrentCultureIgnoreCase)).Configure(t => t.IsKey());

            ///如果实体结尾有s 则不另外添加s
            this.Types().Configure(t =>
            {
                string strClrType = t.ClrType.Name.Substring(0, t.ClrType.Name.LastIndexOf("Entity"));
                if (strClrType.LastIndexOf("s") >= strClrType.Length - 1)
                {
                    t.ToTable("BaiTu_" + strClrType);
                }
                else
                {
                    t.ToTable("BaiTu_" + strClrType + "s");
                }

            });
        }

    }

}
